package com.cy.springcloudalibaba.consumer;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Service;

/**
 * @Description: 接收消息
 * @ClassName: ReceiverService
 * @Author: chenyu
 * @Date: 2025/3/3 15:15
 */
@Service
public class ReceiverService {
    @Autowired
    private Sink sink;

    public void receive() {
        sink.input().subscribe((Message<?> message) -> {
            System.out.println(message.getPayload());
        });
    }

    /**
     *  第二种接收消息方式，通过注解监听接收
     * @param msg 消息体
    */
    @StreamListener(value = Sink.INPUT)
    public void receive2(String msg) {
        System.out.println(msg);
    }
}
